package com.iwe.crm.base.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Enumeration;

@Controller
public class VieController {

    //跳转到登录界面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/login";
    }

    //注销
    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        session.removeAttribute("user");
        session.invalidate();
        return "/login";
    }

    //负责项目中页面跳转的方法
    @RequestMapping({"/toView/{firstView}/{secondView}",
            "/toView/{firstView}/{secondView}/{thirdView}",
            "/toView/{firstView}/{secondView}/{thirdView}/{fourthView}"})
    public String toView(@PathVariable("firstView") String firstView,
                         @PathVariable("secondView") String secondView,
                         @PathVariable(value = "thirdView",required = false)String thirdView,
                         @PathVariable(value = "fourthView",required = false)String fourthView,
                         HttpServletRequest request){

        //获取客户端所有请求参数的名字
        Enumeration<String> parameterNames = request.getParameterNames();

        while(parameterNames.hasMoreElements()){
            String parameterName = parameterNames.nextElement();
            request.setAttribute(parameterName,request.getParameter(parameterName));
        }
        if(fourthView != null){
            return firstView + File.separator + secondView +
                    File.separator + thirdView + File.separator + fourthView;
        }else if(thirdView != null){
            return firstView + File.separator + secondView + File.separator + thirdView;
        }
        return firstView + File.separator + secondView;
    }
}
